fix error reading lowranceusr4 strings found by test-all gcc undefined behavior sanit...
authortsteven4 <tsteven4@gmail.com>
Sun, 16 Aug 2015 17:39:16 +0000 (11:39 -0600)
committertsteven4 <tsteven4@gmail.com>
Sun, 16 Aug 2015 17:39:16 +0000 (11:39 -0600)
gpsbabel/lowranceusr4.cc

index 561b7d481c9a900b8b5a538dc5bb35fbe499f8ba..fde70a824740b02a293504d525b5c0b19fab90ae 100644 (file)
@@ -78,17 +78,18 @@ static int
 lowranceusr4_readstr(char* buf, const int maxlen, gbfile* file, int bytes_per_char)
 {
   int org, len;
+  int bytesread = 0;
 
-  org = len = gbfgetint32(file);
+  org = len = gbfgetint32(file); /* bytes */
   if (len < 0) {
     buf[0] = '\0'; /* seems len=-1 means no string */
     return 0;
   } else if (len) {
-    if (len/bytes_per_char > maxlen) {
+    if (len > maxlen*bytes_per_char) {
       len = maxlen*bytes_per_char;
     }
     if (bytes_per_char == 1) {
-      (void) gbfread(buf, 1, len, file);
+      bytesread += gbfread(buf, 1, len, file);
     } else {
       /* simple adjustment to read strings where characters are 16
          bits (or more).  for now let's just project the characters
@@ -97,15 +98,15 @@ lowranceusr4_readstr(char* buf, const int maxlen, gbfile* file, int bytes_per_ch
       int i, j;
       char discard;
       for (i = 0; i < len/bytes_per_char; ++i) {
-        gbfread(&buf[i], 1, 1, file);
+        bytesread += gbfread(&buf[i], 1, 1, file);
         for (j = 1; j < bytes_per_char; ++j) {
-          gbfread(&discard, 1, 1, file);
+          bytesread +=gbfread(&discard, 1, 1, file);
         }
       }
       buf[len/bytes_per_char] = '\0';
     }
-    if (org > maxlen) {
-      (void) gbfseek(file, bytes_per_char * (org - maxlen), SEEK_CUR);
+    if (org > bytesread) {
+      (void) gbfseek(file, (org - bytesread), SEEK_CUR);
     }
   }